module.exports = {
  css: {
    loaderOptions: {
      stylus: {
        "resolve url": true,
        import: ["./src/theme"]
      }
    }
  },
  pluginOptions: {
    "cube-ui": {
      postCompile: true,
      theme: true
    }
  },
  configureWebpack: {
    devServer: {
      before(app) {
        //node的中间件
        //通过这个中间件，在其它接口执行之前，先执行一个任务
        //在这个任务中，对令牌提前进行校验，
        //若令牌不存在，服务器一般处理，返回一个状态码401，
        //指名当前用户没有权限
        app.use(function(req, res, next){
          //检查token
          if(/^\/api/.test(req.path)) {
            //只校验/api开头的请求
            if(req.path == '/api/login' || req.headers.token) {
              //若果请求的path是来登陆的，或者请求头带有令牌，或者请求的不是api
              next()
            } else {
              res.sendStatus(401); //错误状态提示用户需要登陆
            }
          } else { //中间一定要加一个else，中间在所有请求之前被先执行
            //若不是api请求，而是普通页面请求,要给它继续执行
            //不加，会本地服务的首页都打不开
            next()
          }
        });

        //轮播图，购物车接口
        app.get("/api/goods", function (req, res) {
          res.json({
            code: 0,
            slider: [
              {
                id: 21,
                img: "/img/01.jpg"
              },
              {
                id: 22,
                img: "/img/02.jpg"
              },
              {
                id: 23,
                img: "/img/03.jpg"
              },
              {
                id: 24,
                img: "/img/04.jpg"
              }
            ],
            data: {
              fe: [
                {
                  id: 1,
                  title: "Vue2.x实战",
                  price: "100",
                  img: "/img/01.jpg",
                  count: 100
                },
                {
                  id: 2,
                  title: "React16.x实战",
                  price: "120",
                  img: "/img/03.jpg",
                  count: 100
                },
                {
                  id: 3,
                  title: "nodejs实战",
                  price: "80",
                  img: "/img/02.jpg",
                  count: 100
                },
                {
                  id: 4,
                  title: "前端工程化",
                  price: "110",
                  img: "/img/04.jpg",
                  count: 100
                },
                {
                  id: 5,
                  title: "面试",
                  price: "200",
                  img: "/img/02.jpg",
                  count: 100
                },
                {
                  id: 6,
                  title: "前端安全",
                  price: "30",
                  img: "/img/05.jpg",
                  count: 100
                }
              ],
              python: [
                {
                  id: 7,
                  title: "Python基础语法",
                  price: "120",
                  img: "/img/03.jpg",
                  count: 101
                },
                {
                  id: 8,
                  title: "Flask实战",
                  price: "80",
                  img: "/img/02.jpg",
                  count: 100
                },
                {
                  id: 9,
                  title: "Django实战",
                  price: "110",
                  img: "/img/01.jpg",
                  count: 100
                },
                {
                  id: 10,
                  title: "Python语法进阶",
                  price: "200",
                  img: "/img/04.jpg",
                  count: 100
                }
              ],
              java: [
                {
                  id: 11,
                  title: "java入门实战",
                  price: "80",
                  img: "/img/02.jpg",
                  count: 100
                },
                {
                  id: 12,
                  title: "spring boot实战",
                  price: "110",
                  img: "/img/01.jpg",
                  count: 100
                },
                {
                  id: 13,
                  title: "Java高并发",
                  price: "30",
                  img: "/img/04.jpg",
                  count: 100
                }
              ],
              bigdata: [
                {
                  id: 14,
                  title: "大数据实战",
                  price: "200",
                  img: "/img/01.jpg",
                  count: 100
                },
                {
                  id: 15,
                  title: "Hadoop实战",
                  price: "120",
                  img: "/img/03.jpg",
                  count: 100
                },
                {
                  id: 16,
                  title: "Kafka平台",
                  price: "80",
                  img: "/img/02.jpg",
                  count: 100
                }
              ],
              ai: [
                {
                  id: 17,
                  title: "算法实战",
                  price: "100",
                  img: "/img/01.jpg",
                  count: 100
                },
                {
                  id: 18,
                  title: "个性化推荐",
                  price: "120",
                  img: "/img/03.jpg",
                  count: 100
                },
                {
                  id: 19,
                  title: "机器学习",
                  price: "80",
                  img: "/img/02.jpg",
                  count: 100
                },
                {
                  id: 20,
                  title: "AI实战",
                  price: "110",
                  img: "/img/05.jpg",
                  count: 100
                }
              ]
            },
            keys: ["fe", "python", "java", "bigdata", "ai"]
          });
        });
        app.get("/api/login", function (req, res) {
          //get请求从req.query里
          //post请求从req.body里拿
          console.log('req.query',req.query);
          console.log('req.body',req.body);
          
          const { username, password } = req.query;
          if (username === "lpy" && password === "123") {
            res.json({
              code: 0,
              token: "daan"
            });
          } else {
            res.json({
              code: 1,
              message: "用户名或密码错误"
            });
          }
        });
        // 处理post请求
        app.post('/api/login', function(req, res){
          console.log("用户登录，post");
          let body = [];

          req.on('data', chunk => {
            //接收到一部分数据
            console.log(chunk); //chunk是Buffer对象
            body.push(chunk);
          })
          .on("end", () => {
            //接收到end事件， 说明数据接收完毕
            //将body转换为完整的buffer
            body = Buffer.concat(body).toString(); // utf-8的json字符串
            console.log("body",body);
            //转换并保存前台传递的user
            const { username, password } = JSON.parse(body); // {name: 'aa', age:11}
            if (username === "lpy" && password === "123") {
              res.json({
                code: 0,
                token: "daan"
              });
            } else {
              res.json({
                code: 1,
                message: "用户名或密码错误"
              });
            }
          })
        })

        app.get('/api/logout', function (req, res) {
          res.json({ code: -1 }) //注销登陆
        })


      }
    }
  }
};
